import { _decorator, find, Label, UITransform } from 'cc';
import Dialog from './Dialog';
import { Global } from '../../Util/Global';
import SoundCtrl from '../../Ctrl/SoundCtrl';
const { ccclass, property } = _decorator;

@ccclass('GameOverTipDialog')
export class GameOverTipDialog extends Dialog {
    @property(UITransform)
    bgTran: UITransform = null;

    setData(data: { overType: number }) {
        this.checkFreeRevive();
        return this;
    }

    onLeftBtnClickCall() {
        SoundCtrl.playEffect('mouseclick1');
        Global.gameManager.ResetGame();
        Global.gameManager.GameStart();
        this.close();
    }

    async onRightBtnClickCall() {
        SoundCtrl.playEffect('mouseclick1');
        if (!Global.gameManager.gameover) {
            Global.gameManager.uiCtrl.economicViewUI.target_economic.
                AddMineRes(200 * Global.gameManager.playerData.level);
        }
        else {
            let pos = Global.gameManager.mapCtrl.mapSpawner.RandomPos();
            Global.gameManager.GameStart(pos);
        }
        this.close();
    }

    private show = false;
    onClickTitle() {
        this.show = !this.show;
        this.bgTran.height = this.show ? 180 : 100;
        this.node.getChildByName('leftBtn').active = this.show;
        this.node.getChildByName('rightBtn').active = this.show;
    }

    checkFreeRevive() {
        let alive = Global.gameManager.playerUnit && !Global.gameManager.playerUnit.death;
        let playerData = Global.gameManager.playerData;
        if (alive) {
            let txt = find('rightBtn/txt', this.node).getComponent(Label);
            txt.string = '补充' + 200 * playerData.level;
        }
        else if (Global.gameManager.rewardAdTime > 0) {
            let txt = find('rightBtn/txt', this.node).getComponent(Label);
            // txt.string = Tools.secondsToTimeString(Global.gameManager.rewardAdTime);
            txt.string = '复活';
        }
    }
}


